<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;


        // 报单区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {

            $this->upPrice();
            $this->upChain();
        }

    }

    public function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        // 股东
        if ($amount >= $this->set->vip3) {
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];

                $this->user->save();
            }
        }
        // 会员
        else if ($amount >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];

                $this->user->save();
            }
        }



    }

    public function upChain()
    {
        $chain = C::getReferrerChain($this->user->id);

        foreach ($chain as $user) {
            if ($user->level < UserLevel::$keys['vip1']) {
                continue;
            }

            // 合伙人
            if ($user->level < UserLevel::$keys['vip2']) {
                $children = (int)User::find()
                    ->where(['referrer_id' => $user->id])
                    ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
                    ->count();
                if ($children >= $this->set->vip2) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                }
            }

            // 股东
            if ($user->level < UserLevel::$keys['vip3']) {
                $children = (int)User::find()
                    ->where(['referrer_id' => $user->id])
                    ->andWhere(['>=', 'level', UserLevel::$keys['vip2']])
                    ->count();
                if ($children >= $this->set->vip3_2) {
                    $user->level = UserLevel::$keys['vip3'];
                    $user->save();
                }
            }

            if ($user->level == UserLevel::$keys['vip3']) {

                // v4
                if ($user->v4 == 0) {
                    $amount = $this->getAmount($user->id);
                    if ($amount >= $this->set->v4 * 10000) {
                        $user->v4 = 1;
                        $user->save();
                    }
                }

                // v3
                if ($user->v3 == 0) {
                    $amount = $this->getAmount($user->id);
                    if ($amount >= $this->set->v3 * 10000) {
                        $user->v3 = 1;
                        $user->save();
                    }
                }

                // v2
                if ($user->v2 == 0) {
                    $amount = $this->getAmount($user->id);
                    if ($amount >= $this->set->v2 * 10000) {
                        $user->v2 = 1;
                        $user->save();
                    }
                }

                // v1
                if ($user->v1 == 0) {
                    $amount = $this->getAmount($user->id);
                    if ($amount >= $this->set->v1 * 10000) {
                        $user->v1 = 1;
                        $user->save();
                    }
                }

            }


        }
    }

    // 业绩
    private function getAmount($user_id)
    {
        $ids = User::find()
            ->where(['referrer_id' => $user_id])
            ->select(['id'])
            ->column();
        $ids = array_merge([$user_id], $ids);

        $total = (float)Order::find()
            ->where(['user_id' => $ids])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->sum('total_pay');

        return $total;

        // var_dump($total);exit;
    }
}